<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;
use app\admin\model\UserModel;
use \app\admin\controller\Permissions;
use app\admin\model\AccountModel;
use \think\Db;
use excel\ExportService;
class Account extends Permissions
{
    public function index()
    {	
    	$param = input('param.');
    	$where = [];
        $and_where = [];
    	$user_where = [];
    	
        if (isset($param['phone']) && !empty($param['phone'])) {
            $uid=UserModel::where(['phone'=>$param['phone']])->value('id');
            //$where['uid'] = ['like', '%' . $param['phone'] . '%'];
            $where['uid'] = $uid;
        }
        if (isset($param['status']) && !empty($param['status'])) {
            $where['class'] = $param['status'];
        }
        if (isset($param['pphone']) && !empty($param['pphone'])) {
            $where['source_id'] = UserModel::where(['phone'=>$param['pphone']])->value('id');
        }

        if (isset($param['start']) && !empty($param['start'])) {
            // $param['start'] = strtotime($param['start']);
            $where['create_time'] = ['>=', $param['start']];
        }
        if (isset($param['end']) && !empty($param['end'])) {
            // $param['end'] = strtotime($param['end'] . ' 23:59:59');
            $and_where['create_time'] = ['<=', $param['end']];
        }
        $where['type']=['in',[1,2]];
    	//实例化提现类
    	$model=new AccountModel();
    	$data = $model->where($where)->where($and_where)->order('id desc')->paginate(10,false,['query'=>$this->request->param()]);
    	foreach ($data as $key => $value) {
            $puser=UserModel::where(['id'=>$value['source_id']])->field('nickname,phone')->find();
            $data[$key]['puser']=$puser['nickname'];
            $data[$key]['pphone']=$puser['phone'];
            $data[$key]['user']=$value->users->nickname;
            $data[$key]['phone']=$value->users->phone;
            $data[$key]['money']=$value['money'];
            $data[$key]['balance']=$value['balance'];
            $data[$key]['message']=$value['message'];
            $data[$key]['type']=$value['type'] ==1?'动态账户':'矿池账户';
            $data[$key]['class']=$value['class']==1?'转入' :'转出';
            $data[$key]['create_time']=$value['create_time'];
    	}
    	$this->assign('param',json_encode($param)??"");
        $this->assign('data',$data);
        return $this->fetch();
    }



    #导出
    public function toexcel()
    {
       $param = input('param.');
        $where = [];
        $and_where = [];
        
        if (isset($param['phone']) && !empty($param['phone'])) {
            $uid=UserModel::where(['phone'=>$param['phone']])->value('id');
            //$where['uid'] = ['like', '%' . $param['phone'] . '%'];
            $where['uid'] = $uid;
        }
        if (isset($param['status']) && !empty($param['status'])) {
            $where['class'] = $param['status'];
        }

        if (isset($param['start']) && !empty($param['start'])) {
            $param['start'] = strtotime($param['start']);
            $where['created_at'] = ['>=', $param['start']];
        }
        if (isset($param['end']) && !empty($param['end'])) {
            $param['end'] = strtotime($param['end'] . ' 23:59:59');
            $where['created_at'] = ['<=', $param['end']];
        }
        $where['type']=['in',[1,2]];

        //实例化提现类
        $model=new AccountModel();
        $data = $model->where($where)->order('id desc')->select();
        $data = json_decode(json_encode($data),true);
        foreach ($data as $key => $value) {
            $puser=UserModel::where(['id'=>$value['source_id']])->field('nickname,phone')->find();
            $data[$key]['puser']=$puser['nickname'];
            $data[$key]['pphone']=$puser['phone'];
            $data[$key]['user']=$value->userinfo->nickname;
            $data[$key]['phone']=$value->userinfo->phone;
            $data[$key]['money']=$value['money'];
            $data[$key]['balance']=$value['balance'];
            $data[$key]['message']=$value['message'];
            $data[$key]['type']=$value['type'] ==1?'动态账户':'矿池账户';
            $data[$key]['class']=$value['class']==1?'转入' :'转出';
            $data[$key]['create_time']=$value['create_time'];
        }
        $excel = new ExportService();
       
        // $first = ['A1'=>'编号','B1'=>'提现人姓名','C1'=>'账户余额','D1'=>'手机号','E1'=>'金额','F1'=>'手续费','G1'=>'实际到账金额','H1'=>'账户','I1'=>'状态','J1'=>'交易单号','J1'=>'提现类型','J1'=>'提现银行卡id'];
        $first = ['编号','昵称','手机号','对方昵称','对方手机号','金额','剩余数量','类型','状态','备注信息','时间'];
        
        if($data){
            $result = $excel->index($data,$first,"提现.xls","sheet1");
        }
        
        
    }
   
   
}
